﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vaffanculo.Presentation.Component.WizardControl.Wizards.WizardsMDX
{
    /// <summary>
    /// Interaction logic for FieldSelection.xaml
    /// </summary>
    public partial class FieldSelection : System.Windows.Controls.UserControl, IPageComponent
    {
        private readonly List<string> _listTables;
        private ConectDataBase _conectDataBasePostgres;
        public List<string> ListFunction { get; set; }

        public FieldSelection()
        {
            _conectDataBasePostgres = new ConectDataBase();
            InitializeComponent();
            _listTables = new List<string>();
            _listTables.Add("fct_caudales_embalsado_energia_disponible");
            _listTables.Add("dim_centrales_sistema");
            _listTables.Add("dim_tiempo");
            _listTables.Add("Funciones");
            InitExpanderTables();
        }

        private void InitExpanderTables()
        {
            foreach (string listTable in _listTables)
            {
                Expander expander = new Expander();
                expander.Name = string.Format("Expander_{0}",listTable);
                expander.ExpandDirection = ExpandDirection.Down;
                expander.Header = listTable;
                expander.IsExpanded = true;
                PanelListTables.Children.Add(expander);
                FillExpander(expander,listTable);
            }
            
            //PanelListTables.Children.Add()
        }

        private void FillExpander(Expander expander, string tableName)
        {
            if(tableName=="Funciones") return;
            string queryPostgres =
               string.Format(
                   "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{0}';", tableName);
            DataSet result = _conectDataBasePostgres.ExecuteQuery(queryPostgres);
            DataGrid dataGrid = new DataGrid();
            dataGrid.ItemsSource = result.Tables[0].DefaultView;
            dataGrid.Height = 170;
            
            ScrollViewer scrollViewer = new ScrollViewer();
            scrollViewer.Content = dataGrid;

            expander.Content = scrollViewer;
            
            
        }

        public string KeyText
        {
            get { return "Selección de campos"; }
        }
    }
}
